// //类里面遵循严格模式
// class HD{
//     static PENDING = 'pending'
//     static RESOLVED = "resolved"
//     static REJECTED = "rejected"
//     constructor(executor) {
//         //executor是一个函数(resolve, reject) => {}
//
//         this.status = HD.PENDING
//         this.value = null
//         this.callback = []
//         try {
//             executor(this.resolve.bind(this), this.reject.bind(this))
//         } catch (error) {
//             this.reject(error)
//         }
//     }
//
//     resolve(value) {
//         //pending时才会改变状态
//         if(this.status === HD.PENDING) {
//             this.status = HD.RESOLVED
//             this.value = value
//             //then应该在在execute的下一轮宏任务执行，所以加setTimeout
//             setTimeout(() => {
//                 this.callback.map(callback => {
//                     callback.onFulfilled(value)
//                 })
//             })
//         }
//
//     }
//
//     reject(reason) {
//         if(this.status === HD.PENDING) {
//             this.status = HD.REJECTED
//             this.value = reason
//             setTimeout(() => {
//                 this.callback.map(callback => {
//                     callback.onRejected(reason)
//                 })
//             })
//         }
//     }
//
//     then(onFulfilled, onRejected) {
//         //处理then不传参的情况
//         if(typeof onFulfilled !== 'function') {
//             onFulfilled = () => this.value
//         }
//         if(typeof onRejected !== 'function') {
//             onRejected = () => this.value
//         }
//
//         //将处理三种状态的过程 变成 Promise
//         return new HD((resolve, reject) => {
//             //当resolve异步时，status仍然是pending状态，此时需要将方法推入回调列表
//             if(this.status === HD.PENDING) {
//                 this.callback.push({
//                     //pending错误处理
//                     onFulfilled: value => {
//                         try {
//                             let result = onFulfilled(value)
//                             resolve(result)
//                         } catch (error) {
//                             onRejected(error)
//                         }
//                     },
//                     onRejected: error => {
//                         try {
//                             let result = onRejected(error)
//                             reject(result)
//                         } catch (err) {
//                             onRejected(err)
//                         }
//                     }
//                 })
//             }
//
//             if(this.status === HD.RESOLVED) {
//                 //execute中函数为同步，需要转为异步，加setTimeout
//                 setTimeout(() => {
//                     try {
//                         let result = onFulfilled(this.value)
//                         resolve(result)
//                     } catch (error) {
//                         onRejected(error)
//                     }
//                 }, 0)
//             }
//             if(this.status === HD.REJECTED) {
//                 setTimeout(() => {
//                     try {
//                         let result = onRejected(this.value)
//                         reject(result)
//                     } catch (error) {
//                         onRejected(error)
//                     }
//                 }, 0)
//             }
//         })
//
//
//     }
// }

const PENDING = 'pending'
const RESOLVED = "resolved"
const REJECTED = "rejected"

// function HD(fn) {
//     let self = this
//     this.status = PENDING
//     this.value = null
//     this.callbacks = []
//
//     fn(resolve.bind(this), reject.bind(this))
//
//     function resolve(value) {
//         if(self.status === PENDING) {
//
//             setTimeout(() => {
//                 self.status = RESOLVED
//                 self.value = value
//                 self.callbacks.forEach(callback => {
//                     callback.onFulfilled(self.value)
//                 })
//             }, 0)
//         }
//     }
//
//     function reject(reason) {
//         if(self.status === PENDING) {
//
//             setTimeout(() => {
//                 self.status = REJECTED
//                 self.value = reason
//                 self.callbacks.forEach(callback => {
//                     callback.onRejected(self.value)
//                 })
//             }, 0)
//         }
//     }
// }
//
// HD.prototype.then = function(onFulfilled, onRejected) {
//     if(!onFulfilled) {
//         onFulfilled = () => this.value
//     }
//     if(!onRejected) {
//         onRejected = () => this.value
//     }
//     return new HD((resolve, reject) => {
//         if(this.status === PENDING) {
//             this.callbacks.push({
//                 onFulfilled,
//                 onRejected
//             })
//         }
//         if(this.status === RESOLVED) {
//             setTimeout(() => {
//                 let result = onFulfilled(this.value)
//                 resolve(result)
//             }, 0)
//         }
//         if(this.status === REJECTED) {
//             setTimeout(() => {
//                 let result = onRejected(this.value)
//                 reject(result)
//             }, 0)
//         }
//     })
// }


function MyPromise(fn) {
    const self = this
    this.status = PENDING
    this.callbacks = []
    this.value = null

    fn(resolve.bind(this), reject.bind(this))

    function resolve(value) {
        self.status = RESOLVED
        self.value = value
        self.callbacks.forEach(callback => {
            callback.onFulfilled(value)
        })
    }

    function reject(reason) {
        self.status = REJECTED
        self.value = reason
        self.callbacks.forEach(callback => {
            callback.onRejected(reason)
        })
    }

}

//要注意 不能用匿名函数！！！！没有this了就
MyPromise.prototype.then = function (onFulfilled, onRejected){
    if(typeof onFulfilled !== 'function') {
        onFulfilled = () => this.value
    }
    if(typeof onRejected !== 'function') {
        onRejected = () => this.value
    }
    console.log(onFulfilled, onRejected, this)
    return new MyPromise((resolve, reject) => {
        if(this.status === PENDING) {
            this.callbacks.push({
                onFulfilled,
                onRejected
            })
        }

        if(this.status === RESOLVED) {
            this.setTimeout(() => {
                let result = onFulfilled(this.value)
                resolve(result)
            }, 0)
        }

        if(this.status === REJECTED) {
            this.setTimeout(() => {
                let result = onRejected(this.value)
                reject(result)
            }, 0)
        }
    })
}


console.log(1)
let pr = new MyPromise((resolve, reject) => {
    setTimeout(() => {
        console.log(100)
        resolve("解决")
    }, 1000)
})
console.log(pr)
pr.then((res) => {
    console.log(res)
})